package com.atguigu.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class NettyByteBuf02 {
    public static void main(String[] args) {
        //创建 ByteBuf
        ByteBuf buf = Unpooled.copiedBuffer("hello,world!", Charset.forName("utf-8"));
        //使用相关的方法
        if(buf.hasArray()){//true
            byte[] content = buf.array();
            //将 content 转成字符串
            System.out.println(new String(content,Charset.forName("utf-8")));

            System.out.println("buf=" + buf);
            System.out.println(buf.arrayOffset());//0
            System.out.println(buf.readerIndex());//0
            System.out.println(buf.writerIndex());//18
            System.out.println(buf.capacity());//

//            System.out.println(buf.readByte());
            System.out.println(buf.getByte(0));
            int len = buf.readableBytes();
            System.out.println("len=" + len);

            //使用for取出各个字节
            for (int i = 0; i < len; i++) {
                System.out.println((char)buf.getByte(i));
            }

            System.out.println(buf.getCharSequence(0,4,Charset.forName("utf-8")));
        }
    }
}
